package com.erp.collect.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.erp.collect.domain.bo.ErpCollectProductExpandABo;
import com.erp.collect.domain.vo.ErpCollectProductExpandAVo;
import com.ruoyi.collect.api.domain.ErpCollectProductExpandA;

import com.erp.collect.mapper.ErpCollectProductExpandAMapper;
import com.erp.collect.service.IErpCollectProductExpandAService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采集商品拓展a 从1688采集 由complianceAe方法放入Service业务层处理
 *
 * @author erp
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductExpandAServiceImpl implements IErpCollectProductExpandAService {

    private final ErpCollectProductExpandAMapper baseMapper;

    /**
     * 查询采集商品拓展a 从1688采集 由complianceAe方法放入
     */
    @Override
    public ErpCollectProductExpandAVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询采集商品拓展a 从1688采集 由complianceAe方法放入列表
     */
    @Override
    public TableDataInfo<ErpCollectProductExpandAVo> queryPageList(ErpCollectProductExpandABo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductExpandA> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductExpandAVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采集商品拓展a 从1688采集 由complianceAe方法放入列表
     */
    @Override
    public List<ErpCollectProductExpandAVo> queryList(ErpCollectProductExpandABo bo) {
        LambdaQueryWrapper<ErpCollectProductExpandA> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductExpandA> buildQueryWrapper(ErpCollectProductExpandABo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductExpandA> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductExpandA::getCollectProductId, bo.getCollectProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuProperty()), ErpCollectProductExpandA::getSkuProperty, bo.getSkuProperty());
        lqw.eq(bo.getAbsoluteQuoteType() != null, ErpCollectProductExpandA::getAbsoluteQuoteType, bo.getAbsoluteQuoteType());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpCollectProductExpandA::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getFreightMap()), ErpCollectProductExpandA::getFreightMap, bo.getFreightMap());
        lqw.eq(bo.getIsFreight() != null, ErpCollectProductExpandA::getIsFreight, bo.getIsFreight());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPackSell()), ErpCollectProductExpandA::getIsPackSell, bo.getIsPackSell());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUnit()), ErpCollectProductExpandA::getBaseUnit, bo.getBaseUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getAddUnit()), ErpCollectProductExpandA::getAddUnit, bo.getAddUnit());
        lqw.eq(bo.getAddWeight() != null, ErpCollectProductExpandA::getAddWeight, bo.getAddWeight());
        lqw.eq(bo.getBulkDiscount() != null, ErpCollectProductExpandA::getBulkDiscount, bo.getBulkDiscount());
        lqw.eq(bo.getBulkOrder() != null, ErpCollectProductExpandA::getBulkOrder, bo.getBulkOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrencyCode()), ErpCollectProductExpandA::getCurrencyCode, bo.getCurrencyCode());
        lqw.eq(bo.getDeliveryTime() != null, ErpCollectProductExpandA::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsImageDynamic()), ErpCollectProductExpandA::getIsImageDynamic, bo.getIsImageDynamic());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), ErpCollectProductExpandA::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords1()), ErpCollectProductExpandA::getProductMoreKeywords1, bo.getProductMoreKeywords1());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords2()), ErpCollectProductExpandA::getProductMoreKeywords2, bo.getProductMoreKeywords2());
        lqw.eq(bo.getLotNum() != null, ErpCollectProductExpandA::getLotNum, bo.getLotNum());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerMemberId()), ErpCollectProductExpandA::getOwnerMemberId, bo.getOwnerMemberId());
        lqw.eq(bo.getOwnerMemberSeq() != null, ErpCollectProductExpandA::getOwnerMemberSeq, bo.getOwnerMemberSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageType()), ErpCollectProductExpandA::getPackageType, bo.getPackageType());
        lqw.eq(bo.getStock() != null, ErpCollectProductExpandA::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getClassifyLabel()), ErpCollectProductExpandA::getClassifyLabel, bo.getClassifyLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), ErpCollectProductExpandA::getSummary, bo.getSummary());
        lqw.eq(bo.getProductStatus() != null, ErpCollectProductExpandA::getProductStatus, bo.getProductStatus());
        lqw.eq(bo.getEditStatus() != null, ErpCollectProductExpandA::getEditStatus, bo.getEditStatus());
        lqw.eq(bo.getIsOptimize() != null, ErpCollectProductExpandA::getIsOptimize, bo.getIsOptimize());
        lqw.eq(StringUtils.isNotBlank(bo.getAttributeId()), ErpCollectProductExpandA::getAttributeId, bo.getAttributeId());
        lqw.eq(bo.getTemplateId() != null, ErpCollectProductExpandA::getTemplateId, bo.getTemplateId());
        lqw.eq(bo.getBrandId() != null, ErpCollectProductExpandA::getBrandId, bo.getBrandId());
        lqw.eq(StringUtils.isNotBlank(bo.getProductAttribute()), ErpCollectProductExpandA::getProductAttribute, bo.getProductAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailKeyword()), ErpCollectProductExpandA::getDetailKeyword, bo.getDetailKeyword());
        lqw.eq(bo.getIsAneroidMarkup() != null, ErpCollectProductExpandA::getIsAneroidMarkup, bo.getIsAneroidMarkup());
        lqw.eq(bo.getIsOnlyBattery() != null, ErpCollectProductExpandA::getIsOnlyBattery, bo.getIsOnlyBattery());
        lqw.eq(bo.getIsContainsBattery() != null, ErpCollectProductExpandA::getIsContainsBattery, bo.getIsContainsBattery());
        lqw.eq(bo.getIsTranslate() != null, ErpCollectProductExpandA::getIsTranslate, bo.getIsTranslate());
        lqw.eq(bo.getIsUploadTest() != null, ErpCollectProductExpandA::getIsUploadTest, bo.getIsUploadTest());
        lqw.eq(StringUtils.isNotBlank(bo.getHsCode()), ErpCollectProductExpandA::getHsCode, bo.getHsCode());
        lqw.eq(bo.getIsUpdateTemplate() != null, ErpCollectProductExpandA::getIsUpdateTemplate, bo.getIsUpdateTemplate());
        lqw.eq(bo.getIsAutoOptimize() != null, ErpCollectProductExpandA::getIsAutoOptimize, bo.getIsAutoOptimize());
        lqw.eq(StringUtils.isNotBlank(bo.getAutoOptimizeRemark()), ErpCollectProductExpandA::getAutoOptimizeRemark, bo.getAutoOptimizeRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductExpandA::getRevision, bo.getRevision());
        lqw.eq(bo.getIsViola() != null, ErpCollectProductExpandA::getIsViola, bo.getIsViola());
        lqw.eq(StringUtils.isNotBlank(bo.getViolaText()), ErpCollectProductExpandA::getViolaText, bo.getViolaText());
        lqw.eq(bo.getSkubujiu() != null, ErpCollectProductExpandA::getSkubujiu, bo.getSkubujiu());
        return lqw;
    }

    /**
     * 新增采集商品拓展a 从1688采集 由complianceAe方法放入
     */
    @Override
    public Boolean insertByBo(ErpCollectProductExpandABo bo) {
        ErpCollectProductExpandA add = BeanUtil.toBean(bo, ErpCollectProductExpandA.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采集商品拓展a 从1688采集 由complianceAe方法放入
     */
    @Override
    public Boolean updateByBo(ErpCollectProductExpandABo bo) {
        ErpCollectProductExpandA update = BeanUtil.toBean(bo, ErpCollectProductExpandA.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductExpandA entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采集商品拓展a 从1688采集 由complianceAe方法放入
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
